package com.squirrel.common.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.squirrel.common.entity.UserVideo;
import com.squirrel.common.entity.Video;
import com.squirrel.common.exception.BaseException;
import com.squirrel.common.mapper.UserVideoMapper;
import com.squirrel.common.mapper.VideoMapper;
import com.squirrel.common.param.userVideo.EvaluateParam;
import com.squirrel.common.param.video.PageParam;
import com.squirrel.common.response.code.CodeEnum;
import com.squirrel.common.response.page.PageResult;
import com.squirrel.common.service.UserVideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.squirrel.common.service.VideoService;
import com.squirrel.common.vo.userVideo.HistoryVO;
import com.squirrel.common.vo.userVideo.UserVideoInfoVO;
import com.squirrel.common.vo.video.VideoInfoVO;
import com.squirrel.common.vo.video.VideoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * UserVideoServiceImp 用户视频服务实现类
 * </p>
 *
 * @author 关远键
 * @since 2021/11/11 16:06
 */
@Service
public class UserVideoServiceImp extends ServiceImpl<UserVideoMapper, UserVideo> implements UserVideoService {

    @Autowired
    private UserVideoMapper uvMapper;

    @Autowired
    private VideoService videoService;

    /**
     * 获取喜欢数、收藏数、点赞数
     * @author 关远键
     * @date 2021/11/21 22:27
     * @param videoId 视频ID
     * @return com.squirrel.common.vo.userVideo.VideoInfoVO
     */
    @Override
    public UserVideoInfoVO getByVideoId(Integer videoId) {
        Integer enjoy = uvMapper.enjoyByVideoId(videoId);
        Integer collect = uvMapper.collectByVideoId(videoId);
        Integer like = uvMapper.likeByVideoId(videoId);
        Float score = uvMapper.scoreByVideoId(videoId);
        return new UserVideoInfoVO(videoId, enjoy, collect, like, score);
    }

    /**
     * 视频评价
     * @author 关远键
     * @date 2021/11/22 15:01
     * @param param 评价参数
     * @return int
     */
    @Override
    public int insert(EvaluateParam param) {
        UserVideo userVideo = new UserVideo();
        userVideo.setUserId(param.getUserId());
        userVideo.setVideoId(param.getVideoId());
        userVideo.setWatched(LocalDateTime.of(LocalDate.now(), LocalTime.now()));

        if (param.isEnjoy()) {
            userVideo.setIsEnjoy(1);
        }
        if (param.isCollect()) {
            userVideo.setIsCollect(1);
        }
        if (param.isLike()) {
            userVideo.setIsGiveALike(1);
        }
        if (0 != param.getScore()) {
            userVideo.setScore(param.getScore());
        }

        UserVideo video = uvMapper.get(param.getVideoId(), param.getUserId());
        // 存在时更新
        if (null != video) {
            userVideo.setUserVideoId(video.getUserVideoId());
            if (0 == uvMapper.updateById(userVideo)) {
                throw new BaseException(CodeEnum.DB_EXCEPTION);
            }
        } else {
            // 不存在时插入
            if (0 == uvMapper.insert(userVideo)) {
                throw new BaseException(CodeEnum.DB_EXCEPTION);
            }
        }

        return userVideo.getUserVideoId();
    }

    /**
     * 获取收藏列表
     * @author 关远键
     * @date 2021/11/22 22:52
     * @param userId 用户ID
     * @param pageParam 分页参数
     * @return java.util.List<com.squirrel.common.vo.video.VideoVO>
     */
    @Override
    public PageResult<VideoVO> getCollect(Integer userId, PageParam pageParam) {
        Page<UserVideo> userVideoPage = new Page<>();
        userVideoPage.setSize(pageParam.getSize());
        userVideoPage.setCurrent(pageParam.getNo());


        IPage<UserVideo> page = uvMapper.getCollectByUserId(userVideoPage, userId);
        ArrayList<VideoVO> list = new ArrayList<>();
        for (UserVideo userVideo: page.getRecords()) {
            System.out.println("==============>" + userVideo.toString());
            VideoVO vo = videoService.getByVideoId(userVideo.getVideoId());
            list.add(vo);
        }

        return new PageResult<>(list.size(), list);
    }

    /**
     * 获取用户观看历史
     * @author 关远键
     * @date 2021/11/26 16:12
     * @param userId 用户ID
     * @return java.util.List<com.squirrel.common.vo.userVideo.HistoryVO>
     */
    @Override
    public List<HistoryVO> getHistory(Integer userId) {
        List<UserVideo> list = uvMapper.getHistoryByUserId(userId);

        if (null == list) {
            throw new BaseException(CodeEnum.NON_RESOURCE);
        }

        ArrayList<HistoryVO> history = new ArrayList<>();
        for (UserVideo userVideo: list) {
            VideoVO vo = videoService.getByVideoId(userVideo.getVideoId());

            HistoryVO historyVO = new HistoryVO();
            historyVO.setVideoVO(vo);
            historyVO.setWatched(userVideo.getWatched());

            history.add(historyVO);
        }

        return history;
    }


}
